package com.slj.service.impl;

import com.slj.DTO.BookQueryDTO;
import com.slj.entity.Book;
import com.slj.mapper.BookMapper;
import com.slj.service.BookService;
import com.slj.uitil.Page;
import com.slj.uitil.S;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private  BookMapper bookMapper;


    /**
     * 分页查询书籍
     */
    @Override
    public S<Page<Book>> getBook(@Param("BookQueryDTo") BookQueryDTO bookQueryDTO) {
        Page<Book> page = new Page<>();
        page.setPageNum(bookQueryDTO.getPageNum());
        Integer totalCount = bookMapper.getBookTotalCount(bookQueryDTO.getBookName(), bookQueryDTO.getAuthor());
        page.setTotalCount(totalCount);
        //查询相关数据
        Integer pageNum = (bookQueryDTO.getPageNum() - 1) * bookQueryDTO.getPageSize();
        Integer pageSize = bookQueryDTO.getPageSize();
        List<Book> books = bookMapper.getBookList(bookQueryDTO.getBookName(),
                bookQueryDTO.getAuthor(),
                pageNum,
                pageSize);
        page.setData(books);
        S<Page<Book>> s = S.success(page);
        return s;
    }

    @Override
    public Integer addBook(Book book) {
        return bookMapper.addBook(book);
    }

    @Override
    public Integer deleteBook(Integer id) {
        return  bookMapper.deleteBook(id);
    }

    /**
     * 根据id查询书籍
     *
     * @param id
     */
    @Override
    public S<Book> getBookById(Integer id) {
        Book book = bookMapper.getBookById(id);
        S<Book> s = new S<>();
        s.setData(book);
        return s;
    }

    /**
     * 修改书籍
     *
     * @param book
     */
    @Override
    public Integer updateBook(Book book) {
        return bookMapper.updateBook(book);
    }


}
